import { defineStore } from "pinia";
import {
  listPutPage, //入库单分页查询
} from "@/utils/api/InventoryManagement/InventoryManagement";
import { dictlist } from "@/utils/api/dict/dict"; //字典列表
import { useMemberStore } from "@/stores/modules/member";

const tableHeader = [
  "入库单号",
  "入库类型",
  "入库时间",
  "入库状态",
  "操作人",
  "审批状态",
  "审批人",
  "审批时间",
  "操作",
];
export const useAPputStore = defineStore(
  "APput",
  () => {
    // 定义表头
    const APput_tableHeader = tableHeader;
    //入库单分页列表
    let Putlist: any = [];
    let APput_list = reactive({ Putlist });
    //总条数
    let totalCount: any = ref(0);
    // 入库状态列表
    let StatusList: any = [];
    let putStatusList: any = reactive({ StatusList });
    // 入库类型列表
    let TypeList: any = [];
    let putTypeList: any = reactive({ TypeList });

    /******************** 公共方法 ***********************/
    //获取入库单分页列表
    const getListPutPage = async (data: any) => {
      const userStore: any = useMemberStore();
      // 调用接口获取数据
      const res = await listPutPage({
        pageNum: data.pageNum || 1,
        pageSize: data.pageSize || 10,
        searchParam: data.searchParam || "",
        StartDate: data.StartDate || "",
        EndDate: data.EndDate || "",
        PutStatus: data.PutStatus || "",
        PutType: data.PutType || "",
        ReplyStatuss: ["CheckNot", "CheckFail", "CheckPass"],
        GodownCode: userStore.GodownCode,
        GodownKeeper: userStore.user_id,
      });
      let resdata = res?.data;
      resdata?.list.forEach((item: any) => {
        item.checked = false;
      });
      APput_list.Putlist = resdata?.list;
      totalCount.value = resdata?.totalCount;
      return resdata;
    };
    //获取入库状态列表
    const getDictList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "PutStatus",
      });
      putStatusList.StatusList = res.data;
      return res.data;
    };
    //获取入库类型列表
    const getPutTypeList = async () => {
      const res = await dictlist({
        DictStatus: "Normal",
        DictType: "PutType",
      });
      putTypeList.TypeList = res.data;
      return res.data;
    };
    /*********************** 对外暴露 ********************************/
    return {
      APput_tableHeader,
      getListPutPage,
      APput_list,
      getDictList,
      getPutTypeList,
      putStatusList,
      putTypeList,
      totalCount,
    };
  },
  /**********************************TODO: 持久化***********************/
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
        getItem(key) {
          return uni.getStorageSync(key);
        },
      },
      paths: ["APput_tableHeader", ""],
    },
  }
);
